package cn.starsray.common;

import cn.starsray.constant.ResponseEnum;

public class Response<T> {
    private int code;
    private String msg;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Response<T> err(int code, String msg) {
        return new Response<>(code, msg);
    }

    public Response(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Response<T> err(ResponseEnum responseEnum) {
        if (responseEnum == null) {
            ResponseEnum ERR = ResponseEnum.ERR;
            return new Response<>(ERR.getCode(), ERR.getMsg());
        }
        return new Response<>(responseEnum.getCode(), responseEnum.getMsg());
    }

    public static <T> Response<T> succ(int code, String msg, T data) {
        return new Response<>(code, msg, data);
    }

    public static <T> Response<T> succ(ResponseEnum responseEnum) {
        return new Response<>(ResponseEnum.SUCC.getCode(), ResponseEnum.SUCC.getMsg());
    }

    public static <T> Response<T> succ(ResponseEnum responseEnum, T data) {
        if (responseEnum == null) {
            ResponseEnum SUCC = ResponseEnum.SUCC;
            return new Response<>(SUCC.getCode(), SUCC.getMsg(), data);
        }
        return new Response<>(responseEnum.getCode(), responseEnum.getMsg(), data);
    }
}
